package objects.twodimensional;


import com.dinim.graphics.IGraphicsContext;
import com.dinim.matrix.Matrix;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.matrix.twodimensional.Matrix3x3;

public abstract class Object2D {

	public void rotate(double ang){
		Matrix m = Matrix3x3.generateRotationMatrix(ang);
		multiply(m);
	}
	
	public void translate(double x, double y){
		Matrix m = Matrix3x3.generateTranslationMatrixXY(x, y);
		multiply(m);
	}
	
	public void scale(double x, double y){
		Matrix m = Matrix3x3.generateScalingMatrixXY(x, y);
		multiply(m);
	}
	
	private void multiply(Matrix m){
		IPoint2D[] points = getShape().getPoints();

		Matrix m1 = Matrix3x3.generateTranslationMatrixXY(-getPointOfReference().getX(), -getPointOfReference().getY());
		for(int i = 0; i < points.length; i++){
			Point2D p = (Point2D)points[i];
			p.multiplyByMatrix(m1);
		}
		m.multiplyByMatrix(Matrix3x3.generateTranslationMatrixXY(getPointOfReference().getX(), getPointOfReference().getY()));
		for(int i = 0; i < points.length; i++){
			Point2D p = (Point2D)points[i];
			p.multiplyByMatrix(m);
		}
	}
	
	public void draw(IGraphicsContext gc) {
		IPoint2D[] points = this.getShape().getPoints();
		int[] connections = this.getShape().getConnections();
		for(int i = 0; i < connections.length; i++){
			IPoint2D beg = points[connections[i]];
			IPoint2D end;
			if(i >= connections.length-1){
				end = points[connections[0]];
			}else{
				end = points[connections[i+1]];
			}
			int x1 = (int)Math.round(beg.getX());
			int y1 = (int)Math.round(beg.getY());
			int x2 = (int)Math.round(end.getX());
			int y2 = (int)Math.round(end.getY());
			gc.drawLine(x1, y1, x2, y2);
		}
	}
	
	public abstract IShape2D getShape();
	public abstract IPoint2D getCoordinates();
	public abstract IPoint2D getPointOfReference();

}
